﻿// -----------------------------------------------------------------------
// <copyright file="StronglyTypedCollectionTarget.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Dhgms.TplHelper.Model.DataReaderConsumer
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class StronglyTypedCollectionTarget<TInformationType, TAdoNetHelperClass> : IDataReaderConsumer, ITaskDataResult<IList<TInformationType>>
        where TInformationType : Dhgms.DataManager.Model.Info.InfoBase<TInformationType>
        where TAdoNetHelperClass : Dhgms.DataManager.Model.Helper.AdoNet.AdoNetBase<TInformationType>, new()
    {
        /// <summary>
        /// Gets the data.
        /// </summary>
        public IList<TInformationType> Data { get; set; }

        /// <summary>Processes data reader.</summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns>The number of records processed</returns>
        public long DataReaderAction(IDataReader dataReader)
        {
            if (dataReader == null)
            {
                throw new ArgumentNullException("dataReader");
            }

            var dataCollection = new List<TInformationType>();

            long recordCount = 0;
            var helper = new TAdoNetHelperClass();
            while (dataReader.Read())
            {
                dataCollection.Add(helper.GetStronglyTypedObjectFromDataReaderRow(dataReader));
                recordCount++;
            }

            this.Data = dataCollection;
            return recordCount;
        }
    }
}
